package com.topdt.jbpm.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thx.common.spring.BeanFactory;
import com.thx.file.model.File;
import com.thx.file.service.FileManager;
import com.topdt.jbpm.dao.AttachmentDao;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;
import com.topdt.jbpm.model.WorkAttach;

@Service
@Transactional(readOnly = true)
public class AttachmentService {
	@Autowired
	private AttachmentDao attachmentDao;
	@Autowired
    private FileManager fileService;
	
	
	 public static AttachmentService getAttachmentService() {
	    AttachmentService service = (AttachmentService) BeanFactory.getBean("attachmentService");
	    return service;
	 }

	@Transactional(readOnly = false)
	public void saveOrUpdate(WorkAttach work) {
		attachmentDao.saveOrUpdate(work);
	}

	/**
	 * 根据工作id取得附件列表 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	public List<WorkAttach> getWorkAttachByWorkId(String workId) {
		List<WorkAttach> alist = attachmentDao.getWorkAttachByWorkId(workId);
		if (alist != null) {
			IdentityService idService = IdFactory.getIdentityServiceFactory().getIdentityService();
			for (WorkAttach w : alist) {
				User user = idService.getUserByUserId(w.getHandlerName());
				w.setHandlerName(user.getName());
				
				File file = fileService.get(w.getFileId());
				w.setSysFile(file);
			}
		}
		return alist;
	}

	/**
	 * 删除附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * @return
	 */
	@Transactional(readOnly=false)
	public void deleteAttach(WorkAttach attach) {
	  fileService.delete(attach.getFileId());
	  attachmentDao.delete(attach.getAttachId());
	}

	/**
	 * 根据附件id查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	public WorkAttach getAttachById(String attachId) {
		return attachmentDao.get(attachId);
	}
	/**
	 * 根据附件FileId查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	public WorkAttach getWorkAttachByFileId(String fileId) {
		return attachmentDao.getWorkAttachByFileId(fileId);
	}
	/**
	 * 
	* @author _zili
	* @Description:如果多个附件一起上传，则会出现保存fileId一样的情况，所以再多传个name进行查询。
	* @date 2015年5月6日 上午10:40:54
	 */
	public WorkAttach getWorkAttachByFileIdAndName(String fileId,String realName) {
		return attachmentDao.getWorkAttachByFileIdAndName(fileId, realName);
	}
	//删除工作所有附件
	@Transactional(readOnly=false)
	public void delByWork(String workId) {
		List<WorkAttach> list = getWorkAttachByWorkId(workId);
		if (list != null) {
			for (WorkAttach a : list) {
			  fileService.delete(a.getFileId());
			}
		}
		attachmentDao.delByWorkId(workId);
	}
	
	/**
	 * 建立日期 : Aug 10, 2013 1:54:23 PM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: 根据条件查询附件 <br>
	 * 描述: realName:实际名字；handlerName:处理者;todoId:待办ID<br>
	 * 1 <br>
	 * 2 <br>
	 */
	public List<WorkAttach> getAttachByCondition(String realName,String handlerName,String todoId) {
		return attachmentDao.getAttachByCondition(realName, handlerName, todoId);
	}
	
	/**
	 * 根据work_id删除wf_work_attach表中记录
	 * @author 刘杰
	 * @date   May 15, 2014 3:01:06 PM 
	 * @param delIds
	 */
	@Transactional(readOnly = false)
	public void delWorks(String[] delIds) {
		for (int i = 0; i < delIds.length; i++) {
		  delByWork(delIds[i]);
		}		
	}
}